package schoolsource.util;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import schoolsource.config.ConfigurationItems;

public class SchoolLogger {
	// Set up the logger
	private static String name = "School2Log";
	public static Logger logger = Logger.getLogger(name);
	static {
		try {
			boolean add = true;
			// FileHandler fh = new FileHandler("~/" + name + "%u.log",
			// 10000000, 5,true);
			// FileHandler fh = new FileHandler("/home/mystud2/school_logs/" +
			// name + "%u.log", 10000000,5,true);
			// FileHandler fh = new FileHandler(ConfigurationItems.LOGPATH +
			// name + "%u.log", 10000000,5,true);
			FileHandler fh = new FileHandler("bob.log", 1000000, 5, true);
			// The following line changes the default file format
			// from XML to a non-tagged format
			fh.setFormatter(new SimpleFormatter());

			Handler h[] = logger.getHandlers();
			if (null != h) {
				for (int i = 0; i < h.length; i++) {
					if (h[i].getClass().toString().equals("class java.util.logging.FileHandler")) {
						add = false;
						logger.log(Level.INFO, "Found a FileHandler");
					} else {
						logger.log(Level.INFO, "Found a non-FileHandler: " + h[i].getClass());
						logger.removeHandler(h[i]);
					}
				}
			}
			if (add) {
				logger.log(Level.INFO, "Adding a FileHandler");
				logger.addHandler(fh);
			}
		} catch (IOException ioe) {
			System.out.println("GOT ERROR: " + ioe);
			ioe.printStackTrace();
		}
	}

}
